$(function(){
    const $name      = $("#name");
    const $phone     = $("#phone");
    const $idNumber  = $("#idNumber");
    const $email     = $("#email");
    const $passwd    = $("#passwd");
    const $input     = $("input");
    const $addr      = $("#addr");

    const $labelName     = $("#label-name");
    const $labelPhone    = $("#label-phone");
    const $labelIdNumber = $("#label-idNumber");
    const $labelEmail    = $("#label-email");
    const $labelPasswd   = $("#label-passwd");

    const submit = function(){
        $name.val($name.val().trim());
        $phone.val($phone.val().trim());
        $idNumber.val($idNumber.val().trim());
        $email.val($email.val().trim());
        if (!test_ipt(testRealName,$name,$labelName)){
            layer.msg('请输入真实姓名');
            $name.focus();
            return false;
        }
        if (!test_ipt(testPhone,$phone,$labelPhone)){
            layer.msg('请输入正确的手机号码');
            $phone.focus();
            return false;
        }
        if (!test_ipt(testIdNumber,$idNumber,$labelIdNumber)){
            layer.msg('请输入正确的身份证号码');
            $idNumber.focus();
            return false;
        }
        if (!test_ipt(testEmail,$email,$labelEmail)){
            layer.msg('请输入正确的电子邮箱');
            $email.focus();
            return false;
        }
        if (!test_ipt(testPasswd,$passwd,$labelPasswd)){
            layer.msg('密码格式有误');
            $passwd.focus();
            return false;
        }
        let windowId = layer.load();
        $.getJSON("/admin/views/courier/add.do", {
            name:$name.val(),
            phone:$phone.val(),
            idNumber:$idNumber.val(),
            email:$email.val(),
            passwd:$passwd.val(),
            addr:$addr.val()
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status > 0) {
                $input.val("");
                $addr.val("北京");
            } else if (data.status === -404) {
                location.assign('/login.html');
            }
        });
        return false;
    }
    {
        $name.bind('input propertychange', function () {
            test_ipt(testRealName, $name, $labelName);})
        $phone.bind('input propertychange', function () {
            test_ipt(testPhone, $phone, $labelPhone);})
        $idNumber.bind('input propertychange', function () {
            test_ipt(testIdNumber, $idNumber, $labelIdNumber);})
        $email.bind('input propertychange', function () {
            test_ipt(testEmail, $email, $labelEmail);})
        $passwd.bind('input propertychange', function () {
            test_ipt(testPasswd, $passwd, $labelPasswd);})
    }
    setEnterKeyEvent($input, submit);
    showTips($passwd, $labelPasswd);
    $("#submit").click(submit);
    $("#reset").click(function () {
        $input.val("");
        $addr.val("北京");
    })
});